package com.lisx.module_add_new_word.utils;
import android.content.Context;

import com.lisx.module_add_new_word.bean.MyDatabase;
import com.lisx.module_add_new_word.bean.WordBean;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;


public class QueryUtils {

	//读取本地json文件转换为string
	public static String getJson(String fileName, Context context){
		StringBuilder stringBuilder = new StringBuilder();
		try {
			InputStream is = context.getAssets().open(fileName);
			BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(is));
			String line;
			while ((line=bufferedReader.readLine()) != null){
				stringBuilder.append(line);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}

		return stringBuilder.toString();
	}

	public static List<WordBean> queryWord(Context context) {
		MyDatabase myDatabase = MyDatabase.getInstance(context);
		List<WordBean> wordBeans = new ArrayList<>();
		String data = getJson("zi.json",context);
		String id;
		String zi_content;
		String is_start;
		String word;
		String tips;
		try {
			JSONArray jsonArray = new JSONArray(data);
			System.out.println("lll=========="+jsonArray.length());
			for (int i = 0;i < jsonArray.length();i++){
				id = jsonArray.getJSONObject(i).getString("id");
				zi_content = jsonArray.getJSONObject(i).getString("zi_content");
				is_start = jsonArray.getJSONObject(i).getString("is_start");
				word = jsonArray.getJSONObject(i).getString("word");
				tips = jsonArray.getJSONObject(i).getString("tips");
				WordBean wordBean = new WordBean();
				wordBean.setId(i+1);
				wordBean.setIds(id);
				wordBean.setIs_start(is_start);
				wordBean.setTips(tips);
				wordBean.setWord(word);
				wordBean.setZi_content(zi_content);
				if (i < 4){
					wordBean.setSelect(1);
				}else {
					wordBean.setSelect(0);
				}

				wordBeans.add(wordBean);
				myDatabase.wordBeanDao().insertWordBean(wordBean);
			}

		} catch (JSONException e) {
			e.printStackTrace();
		}
		return wordBeans;
	}

	public static String queryAnswer(Context context,String id) {
		MyDatabase myDatabase = MyDatabase.getInstance(context);
		List<WordBean> wordBeans = new ArrayList<>();
		String data = getJson("suanfa.json",context);
		String child = "";
		try {
			JSONObject jsonObject = new JSONObject(data);
			child = jsonObject.getJSONObject(id).getString("child");
			System.out.println("child=========="+child);
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return child;
	}

	public static String queryTips(Context context,String id) {
		MyDatabase myDatabase = MyDatabase.getInstance(context);
		List<WordBean> wordBeans = new ArrayList<>();
		String data = getJson("suanfa.json",context);
		String child = "";
		try {
			JSONObject jsonObject = new JSONObject(data);
			child = jsonObject.getJSONObject(id).getString("child");
			System.out.println("child=========="+child);
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return child;
	}




}
